# Download Visual Studio build tools
if (!(Test-Path C:\BuildTools)) {
    (New-Object System.Net.WebClient).DownloadFile('https://aka.ms/vs/16/release/vs_buildtools.exe', 'vs_buildtools.exe')

    .\vs_buildtools.exe --quiet --wait --norestart --nocache `
        --installPath C:\BuildTools `
        --add Microsoft.VisualStudio.Workload.VCTools `
        --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 `
        --add Microsoft.VisualStudio.Component.Windows10SDK.18362
}

# Download the MSYS2 
if (!(Test-Path C:\msys64)) {
    (New-Object System.Net.WebClient).DownloadFile('https://github.com/msys2/msys2-installer/releases/download/2020-06-29/msys2-base-x86_64-20200629.sfx.exe', 'msys2.exe')
    .\msys2.exe -y -oC:\  # Extract to C:\msys64
    Remove-Item msys2.exe  # Delete the archive again

    # Run for the first time
    C:\msys64\usr\bin\bash -lc ' '
    # Update MSYS2
    C:\msys64\usr\bin\bash -lc 'pacman -Syuu --noconfirm'  # Core update (in case any core packages are outdated)
    C:\msys64\usr\bin\bash -lc 'pacman -Syuu --noconfirm'  # Normal update
    C:\msys64\usr\bin\bash -lc 'pacman -Sy --noconfirm mingw-w64-x86_64-toolchain mingw-w64-i686-toolchain mingw-w64-x86_64-yasm mingw-w64-i686-yasm'
    C:\msys64\usr\bin\bash -lc 'pacman -Sy --noconfirm make pkg-config diffutils'
    C:\msys64\usr\bin\bash -lc 'mv /usr/bin/link /usr/bin/link_bk'
}
